/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "Game/Server/WorldSession.h"
#include "Ucop/Data/Rel/SessionImpl.h"
#include "Shared/Log/Log.h"
#include "Shared/Typedefs.h"
#include "Game/Server/WorldSession.h"
#include "Game/Global.h"
#include "Game/Game/Pet.h"
#include "Game/Game/Player.h"


class CharacterHandler
{
public:
	void HandleCharEnumCallback(QueryResult* _Result, std::uint32_t _Account)
	{
		QueryResultPtr _ResultPtr(_Result);
		WorldSession* _Session = sWorld.FindSession(_Account);
		if (!_Session) {
			return;
		}
		_Session->HandleCharEnum(_Result);
	}
} chrHandler;

void WorldSession::HandleCharEnumOpcode(WorldPacket &_Packet)
{
	sLog.outDebug("enter GameSession::handleCharEnumOpcode()");

	sCharaDB.AsyncPQuery(&chrHandler, &CharacterHandler::HandleCharEnumCallback, GetAccountId(),
		//           0               1                2                3                 4                  5                       6                        7
		"SELECT characters.guid, characters.name, characters.race, characters.class, characters.gender, characters.playerBytes, characters.playerBytes2, characters.level, "
		//   8                9               10                     11                     12                     13                    14
		"characters.zone, characters.map, characters.position_x, characters.position_y, characters.position_z, guild_member.guildid, characters.playerFlags, "
		//  15                    16                   17                     18                   19
		"characters.at_login, character_pet.entry, character_pet.modelid, character_pet.level, characters.equipmentCache "
		"FROM characters LEFT JOIN character_pet ON characters.guid=character_pet.owner AND character_pet.slot='%u' "
		"LEFT JOIN guild_member ON characters.guid = guild_member.guid "
		"WHERE characters.account = '%u' ORDER BY characters.guid",
		PET_SAVE_AS_CURRENT, GetAccountId());
}

void WorldSession::HandleCharEnum(QueryResult *_Rs)
{
	WorldPacket _Data(SMSG_CHAR_ENUM, 100);
	std::uint8_t _Num = 0;

	_Data << _Num;
	if (_Rs)
	{
		do
		{
			std::uint32_t _Guidlow = (*_Rs)[0].AsUInt32();
			sLog.outDebug("build enum data for char guid %u from account %u.", _Guidlow, GetAccountId());
            if (Player::BuildCharaEnumData(_Rs, _Data))
			{
				++_Num;
			}
		} while (_Rs->NextRow());
	}

	_Data.Put<std::uint8_t>(0, _Num);
	SendPacket(_Data);
}

void WorldSession::HandleCharCreateOpcode(WorldPacket &_Packet)
{

}